using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The TravelDocumentTypeCodeTableDB class is responsible for interacting with the database to retrieve and store information
/// about TravelDocumentTypeCodeTable objects.
/// </summary>
public class TravelDocumentTypeCodeTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of TravelDocumentTypeCodeTable from the underlying datasource.
/// </summary>
/// <param name="travelDocTypeCode">The unique TravelDocTypeCode of the TravelDocumentTypeCodeTable in the database.</param>
/// <returns>An TravelDocumentTypeCodeTable when the TravelDocTypeCode was found in the database, or null otherwise.</returns>
public static TravelDocumentTypeCodeTable GetItem(Int16 travelDocTypeCode)
{
TravelDocumentTypeCodeTable myTravelDocumentTypeCodeTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTravelDocumentTypeCodeTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@travelDocTypeCode", travelDocTypeCode);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myTravelDocumentTypeCodeTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myTravelDocumentTypeCodeTable;
}
}

/// <summary>
/// Returns a list with TravelDocumentTypeCodeTable objects.
/// </summary>
/// <returns>A generics List with the TravelDocumentTypeCodeTable objects.</returns>
public static TravelDocumentTypeCodeTableList GetList()
{
TravelDocumentTypeCodeTableList tempList = new TravelDocumentTypeCodeTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTravelDocumentTypeCodeTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a TravelDocumentTypeCodeTable in the database.
/// </summary>
/// <param name="myTravelDocumentTypeCodeTable">The TravelDocumentTypeCodeTable instance to save.</param>
/// <returns>The new TravelDocTypeCode if the TravelDocumentTypeCodeTable is new in the database or the existing TravelDocTypeCode when an item was updated.</returns>
public static Int16 Save(TravelDocumentTypeCodeTable myTravelDocumentTypeCodeTable)
{
Int16 result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTravelDocumentTypeCodeTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myTravelDocumentTypeCodeTable.TravelDocTypeCode == -1)
{myCommand.Parameters.AddWithValue("@travelDocTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@travelDocTypeCode", myTravelDocumentTypeCodeTable.TravelDocTypeCode);
}
if (myTravelDocumentTypeCodeTable.CategoryType == null){
myCommand.Parameters.AddWithValue("@categoryType", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@categoryType", myTravelDocumentTypeCodeTable.CategoryType);
}
if (myTravelDocumentTypeCodeTable.ShortDescription == null){
myCommand.Parameters.AddWithValue("@shortDescription", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@shortDescription", myTravelDocumentTypeCodeTable.ShortDescription);
}
if (myTravelDocumentTypeCodeTable.Description == null){
myCommand.Parameters.AddWithValue("@description", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@description", myTravelDocumentTypeCodeTable.Description);
}
if (myTravelDocumentTypeCodeTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myTravelDocumentTypeCodeTable.Status);
}
if (myTravelDocumentTypeCodeTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myTravelDocumentTypeCodeTable.CreatedDateTime);
}
if (myTravelDocumentTypeCodeTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myTravelDocumentTypeCodeTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt16(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a TravelDocumentTypeCodeTable from the database.
/// </summary>
/// <param name="travelDocTypeCode">The TravelDocTypeCode of the TravelDocumentTypeCodeTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(Int16 travelDocTypeCode)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTravelDocumentTypeCodeTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@travelDocTypeCode", travelDocTypeCode);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the TravelDocumentTypeCodeTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static TravelDocumentTypeCodeTable FillDataRecord(IDataRecord myDataRecord )
{
TravelDocumentTypeCodeTable myTravelDocumentTypeCodeTable = new TravelDocumentTypeCodeTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TravelDocTypeCode")))
{
myTravelDocumentTypeCodeTable.TravelDocTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("TravelDocTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CategoryType")))
{
myTravelDocumentTypeCodeTable.CategoryType = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CategoryType"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ShortDescription")))
{
myTravelDocumentTypeCodeTable.ShortDescription = myDataRecord.GetString(myDataRecord.GetOrdinal("ShortDescription"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
{
myTravelDocumentTypeCodeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myTravelDocumentTypeCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myTravelDocumentTypeCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myTravelDocumentTypeCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myTravelDocumentTypeCodeTable;
}
}
